######################################################################
### R file to produce the figures in "All Economics Is Local"
###
### Created: 3-19-15
### Modified: 3-2-16
###
######################################################################

library(foreign)
library(ggplot2)
library(lattice)
library(fields)
library(MASS)
library(mnormt)
library(nonnest2)
library(corrplot)

# Set up working directory
#setwd("")


#########################################################################
### Figure 1: Change in the predicted probability of selecting 'worse' given a decrease in the spatial lag for change in state gross state product per capita across specifications of W
#########################################################################
wm <- read.dta("wmodels.dta")

m1 <- subset(wm, model == 1)
m1_g <- subset(wm, model == 1 & outcome == 3 & v == "GSP")
m1_gsl <- subset(wm, model == 1 & outcome == 3 & v == "GSP SL")

g <- ggplot(m1, aes(x=w2)) + ylab("Change in Pr(Worse)") + xlab("") + ggtitle("GSP Per Capita Change \n 3% to 1%")
g <- g + geom_linerange(data=m1_g, aes(ymin=lo, ymax=hi), size=1, color="indianred2")
g <- g + geom_point(data=m1_g, aes(y=d, color="State", shape="State"), size=2.5)
g <- g + geom_linerange(data=m1_gsl, aes(ymin=lo, ymax=hi), size=1, color="blue")
g <- g + geom_point(data=m1_gsl, aes(y=d, color="Spatial Lag", shape="Spatial Lag"), size=2.5)
g <- g + geom_hline(yintercept=0, linetype="dashed")
g <- g + scale_color_manual(values = c("State" = "indianred2", "Spatial Lag"="blue"))
g <- g + theme(legend.title=element_blank(), legend.position="bottom", axis.text.x=element_text(angle=60, hjust=1.25))
g <- g + scale_x_discrete(limits=c("Contiguity", "Cross-Border Employment", "House Co-Sponsorship", "Economic Similarity", "Media Mentions"))
g

#########################################################################
### Figure 2: Correlation of W Matrices
#########################################################################
data <- read.dta("All Economics Is Local.dta")

c <- 1
local <- c()
national <- c()
cont <- c()
cosponsor <- c()
econ <- c()
media <- c()
cross <- c()

for(s in unique(data$state)){
	for(y in unique(data$year)){
		local[c] <- data$gsp_pc2_ch[data$state == s & data$year == y][1]
		cont[c] <- data$sp_W2_gsp_pc2_ch[data$state == s & data$year == y][1]
		cosponsor[c] <- data$sp_W7_gsp_pc2_ch[data$state == s & data$year == y][1]
		econ[c] <- data$sp_W14_gsp_pc2_ch[data$state == s & data$year == y][1]
		media[c] <- data$sp_W10U_gsp_pc2_ch[data$state == s & data$year == y][1]
		cross[c] <- data$sp_W12_gsp_pc2_ch[data$state == s & data$year == y][1]
		national[c] <- data$gdppc_growth[data$state == s & data$year == y][1]

		c <- c + 1
	}
}
	
cors <- data.frame(media, econ, cosponsor, cross, cont, local)
colnames(cors) <- c('Media', 'Economic Similarity', 'Cosponsorship', 'Cross-Border Employment', 'Contiguity', 'Real Local Economy')
cors <- cors[1:300, ]
cors <- cor(cors)	

### Produce the correlation plot
pdf('correlationMatrix.pdf')
	corrplot(cors, method = 'shade', addCoef.col = 'white', cl.pos = 'n', tl.col='black')
dev.off()

